CODE 144. Insertion Sort List

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/11/27/2013-11-27-CODE 144 Insertion Sort List/

访问原文「CODE 144. Insertion Sort List

###
Sort a linked list using insertion sort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public ListNode insertionSortList(ListNode head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode node = new ListNode(0);
ListNode tmp = head;
while (null != tmp) {
ListNode next = tmp.next;
if (node.next == null) {
tmp.next = node.next;
node.next = tmp;
} else {
ListNode tmpNode = node;
while (null != tmpNode.next && tmpNode.next.val <= tmp.val) {
tmpNode = tmpNode.next;
}
tmp.next = tmpNode.next;
tmpNode.next = tmp;
}
tmp = next;
}
return node.next;
}
Jerky Lu wechat
欢迎加入微信公众号